﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Common
{
    /// <summary>
    /// 文件帮助类
    /// </summary>
    public static class FileHepler
    {
        /// <summary>
        /// 将对象保存成JSON格式的文件
        /// </summary>
        /// <param name="value"></param>
        /// <param name="path"></param>
        /// <param name="fileName"></param>
        public static void SaveJsonFile<T>(this T value, String path, String fileName) where T : class
        {
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            var filePath = System.IO.Path.Combine(path, fileName);

            System.IO.File.WriteAllText(filePath, value.ToJsonString());
        }


        /// <summary>
        /// 读取JSON格式文件并转换成对象
        /// </summary>
        /// <param name="value">文件夹</param>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        public static dynamic LoadJsonFile(this string value, string fileName)
        {
            if (!System.IO.Directory.Exists(value))
            {
                System.IO.Directory.CreateDirectory(value);
            }
            var filePath = System.IO.Path.Combine(value, fileName);
            if (System.IO.File.Exists(filePath))
            {
                return System.IO.File.ReadAllText(filePath).ToDynamic();
            }

            return null;
        }

        /// <summary>
        /// 读取JSON格式文件并转换成对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">文件夹</param>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        public static T LoadJsonFile<T>(this string value, string fileName) where T : class
        {
            T obj = default(T);

            if (!System.IO.Directory.Exists(value))
            {
                System.IO.Directory.CreateDirectory(value);
            }

            var filePath = System.IO.Path.Combine(value, fileName);

            if (System.IO.File.Exists(filePath))
            {
                obj = System.IO.File.ReadAllText(filePath).ToObject<T>();
            }

            return obj;
        }

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        public static extern IntPtr _lopen(string lpPathName, int iReadWrite);

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        public static extern bool CloseHandle(IntPtr hObject);
        public const int OF_READWRITE = 2;
        public const int OF_SHARE_DENY_NONE = 0x40;
        public static readonly IntPtr HFILE_ERROR = new IntPtr(-1);

        /// <summary>
        /// 文件是否被打开，打开说明文件被占用
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool IsFileOpen(string path)
        {
            if (!System.IO.File.Exists(path))
            {
                return false;
            }
            IntPtr vHandle = _lopen(path, OF_READWRITE | OF_SHARE_DENY_NONE);//windows Api上面有定义扩展方法
            if (vHandle == HFILE_ERROR)
            {
                return true;//打开的
            }
            CloseHandle(vHandle);
            return false;//关闭的
        }
    }
}
